package example.servlet;

import com.alibaba.fastjson2.JSON;
import example.pojo.StorageRecord;
import example.pojo.Supplier;
import example.service.StorageRecordService;
import example.service.StorageRecordServiceImpl;
import example.service.SupplierService;
import example.service.SupplierServiceImpl;
import example.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/storageRecord")
public class StorageRecordServlet extends HttpServlet {
    private StorageRecordService recordService = new StorageRecordServiceImpl();
    private SupplierService supplierService = new SupplierServiceImpl();
    private PrintWriter out;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码方式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        out = resp.getWriter();
        String opr = req.getParameter("opr");
        if(opr.equals("list")){
            list(req,resp);
        }else if(opr.equals("view")){
            view(req,resp);
        }else if(opr.equals("toUpdate")){
            //获取ID
            int storageRecordId = Integer.parseInt(req.getParameter("storageRecordId"));
            req.setAttribute("storageRecord",recordService.view(storageRecordId));
            req.getRequestDispatcher("/WEB-INF/jsp/storageRecord/update.jsp").forward(req, resp);
        }else if(opr.equals("update")){
            update(req,resp);
        }else if(opr.equals("toAdd")){
            req.getRequestDispatcher("/WEB-INF/jsp/storageRecord/add.jsp").forward(req, resp);
        }else if (opr.equals("add")){
            add(req,resp);
        }else if(opr.equals("del")){
            //获取id
            int storageRecordId = Integer.parseInt(req.getParameter("storageRecordId"));
            if(recordService.del(storageRecordId) > 0){
                out.println("true");
            }else{
                out.println("false");
            }
        }else if(opr.equals("simpleList")){
            List<Supplier> supplierList = supplierService.all();
            if(supplierList != null){
                String json = JSON.toJSONString(supplierList);
                out.println(json);
            }else{
                System.out.println("未获取的供货商信息");
            }
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StorageRecord record = new StorageRecord();
        //获取信息
        String srCode = req.getParameter("srCode");
        record.setSrCode(srCode);
        String goodsName = req.getParameter("goodsName");
        record.setGoodsName(goodsName);
        String goodsUnit = req.getParameter("goodsUnit");
        record.setGoodsUnit(goodsUnit);
        int goodsCount = (int) Double.parseDouble(req.getParameter("goodsCount"));
        record.setGoodsCount(goodsCount);
        int totalAmount = (int) Double.parseDouble(req.getParameter("totalAmount"));
        record.setTotalAmount(totalAmount);
        int supplierId = Integer.parseInt(req.getParameter("supplierId"));
        record.setSupplierId(supplierId);
        int payStatus = Integer.parseInt(req.getParameter("payStatus"));
        record.setPayStatus(payStatus);

        if(recordService.add(record) > 0){
            System.out.println("入库信息添加成功");
            req.getRequestDispatcher("storageRecord?opr=list&pageIndex=1").forward(req,resp);
        }else{
            System.out.println("入库信息添加失败");
            req.getRequestDispatcher("/WEB-INF/jsp/storageRecord/list.jsp").forward(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException , ServletException{
        StorageRecord record = new StorageRecord();
        //获取信息
        int id = Integer.parseInt(req.getParameter("id"));
        record.setId(id);
        String srCode = req.getParameter("srCode");
        record.setSrCode(srCode);
        String goodsName = req.getParameter("goodsName");
        record.setGoodsName(goodsName);
        String goodsUnit = req.getParameter("goodsUnit");
        record.setGoodsUnit(goodsUnit);
        int goodsCount = (int) Double.parseDouble(req.getParameter("goodsCount"));
        record.setGoodsCount(goodsCount);
        int totalAmount = (int) Double.parseDouble(req.getParameter("totalAmount"));
        record.setTotalAmount(totalAmount);
        int supplierId = Integer.parseInt(req.getParameter("supplierId"));
        record.setSupplierId(supplierId);
        int payStatus = Integer.parseInt(req.getParameter("payStatus"));
        record.setPayStatus(payStatus);

        if(recordService.upd(record) > 0){
            System.out.println("入库信息修改成功");
            req.getRequestDispatcher("storageRecord?opr=list&pageIndex=1").forward(req,resp);
        }else{
            System.out.println("入库信息修改失败");
            req.getRequestDispatcher("/WEB-INF/jsp/storageRecord/list.jsp").forward(req, resp);
        }
    }

    private void view(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ID
        int storageRecordId = Integer.parseInt(req.getParameter("storageRecordId"));
        req.setAttribute("storageRecord",recordService.view(storageRecordId));
        req.getRequestDispatcher("/WEB-INF/jsp/storageRecord/view.jsp").forward(req, resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageIndex = Integer.parseInt(req.getParameter("pageIndex"));

        String queryGoodsName = req.getParameter("queryGoodsName");
        String querySupplierIdStr = req.getParameter("querySupplierId");
        String queryPayStatusStr = req.getParameter("queryPayStatus");

        Page<StorageRecord> recordPage = new Page<>();
        //设置总数据量
        recordPage.setTotalPage(recordService.count(queryGoodsName,(querySupplierIdStr == null || querySupplierIdStr.equals("")) ? null : Integer.parseInt(querySupplierIdStr) , (queryPayStatusStr==null || queryPayStatusStr.equals("")) ? null : Integer.parseInt(queryPayStatusStr)));
        //设置总页数
        recordPage.setTotalPageSize();
        //设置当前页
        recordPage.setPageIndex(pageIndex);
        //获取当前页数据
        recordPage.setList(recordService.select(queryGoodsName,(querySupplierIdStr == null || querySupplierIdStr.equals("")) ? null : Integer.parseInt(querySupplierIdStr) , (queryPayStatusStr==null || queryPayStatusStr.equals("")) ? null : Integer.parseInt(queryPayStatusStr) , recordPage.getPageIndex(),recordPage.getPageSize()));

        //获取供货商信息
        List<Supplier> supplierList = supplierService.all();

        //数据回显
        req.setAttribute("supplierList",supplierList);
        req.setAttribute("queryGoodsName",queryGoodsName);
        req.setAttribute("querySupplierId",querySupplierIdStr);
        req.setAttribute("queryPayStatus",queryPayStatusStr);
        req.setAttribute("storageRecordList",recordPage.getList());

        req.setAttribute("totalPageCount",recordPage.getTotalPageSize());
        req.setAttribute("totalCount",recordPage.getTotalPage());
        req.setAttribute("currentPageNo",recordPage.getPageIndex());
        req.getRequestDispatcher("/WEB-INF/jsp/storageRecord/list.jsp").forward(req, resp);
    }
}
